<?php
ini_set("date.timezone", "Asia/Shanghai");
ini_set("memory_limit", "1024M");
//ini_set("memory_limit", "-1");

$prog=new Program();
$prog->run();

class Program
{
    public function run()
    {
        self::test_bin_str();
    }

    private function test_bin_str()
    {
        $str="10aabb28fe9a84023dcf8b50";
        print $str."\n";
        $bin=Misc::CollapseFromString($str);
        print $bin."\n";
        $file=fopen("tmp.txt", "w");
        fwrite($file, $bin);
        fclose($file);
        $str2=Misc::ExpandToString($bin);
        print $str2."\n";
    }
}

class Misc
{
    public static function ExpandToString($bin)
    {/*{{{*/
        $str="";
        foreach(str_split($bin, 1) as $c)
        {
            $str.=sprintf("%02X", ord($c));
        }
        return $str;
    }/*}}}*/

    public static function CollapseFromString($str)
    {/*{{{*/
        $bin="";
        $len=strlen($str);
        if($len % 2 == 0)
        {
            for($i=0; $i<$len; $i+=2)
            {
                $bin.=chr(intval(substr($str, $i, 2), 16));
            }
        }
        return $bin;
    }/*}}}*/
}
?>
